package org.linlinjava.litemall.wx.dto;



import com.alibaba.fastjson.JSON;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.linlinjava.litemall.core.util.HttpUtil;
import org.springframework.web.client.RestTemplate;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;


public class WechatSendTemplate {
    public static String appID = "wxbe20bf8044a8d645";
    public static String appsecret = "054ac5ba4a488ae8673193374016ca43";

    /***
     * 提醒事件查询
     * @param openId
     * @param keyValue1 待完成目标的内容
     */
    public static void sendTemplate(String openId,String keyValue1,String keyValue2,String keyValue3,String remark){
        String token = getAccessToken();
        String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+token;
        WxTemplate wxTemplate = new WxTemplate();
       // wxTemplate.setTemplate_id("这里写上你之前新建的模板id");
        wxTemplate.setTemplate_id("Wctb0VVfwYhdHsZzywD8VC2ZQy6p8pD2ltBUJK6L-hg");

        wxTemplate.setTouser(openId);
        wxTemplate.setUrl("https:www.baidu.com");
        wxTemplate.setTopcolor("#FF0000");
        Map<String, TemplateData> map = new HashMap<>();
        TemplateData first = new TemplateData();
        first.setColor("#868586");
        first.setValue("您的订单已经确认成功");
        map.put("first", first);
        TemplateData keyword1 = new TemplateData();
        keyword1.setColor("#173177");
        keyword1.setValue(keyValue1);
        map.put("keyword1",keyword1);

        TemplateData keyword2 = new TemplateData();
        keyword1.setColor("#173177");
        keyword1.setValue(keyValue2);
        map.put("keyword2",keyword2);

        TemplateData keyword3 = new TemplateData();
        keyword1.setColor("#173177");
        keyword1.setValue(keyValue3);
        map.put("keyword3",keyword3);

        TemplateData remark1 = new TemplateData();
        keyword1.setColor("#173177");
        keyword1.setValue(remark);
        map.put("remark", remark1);

        wxTemplate.setData(map);
        JSONObject json = JSONObject.fromObject(wxTemplate);//将java对象转换为json对象


        String s = JSON.toJSONString(wxTemplate);


        String result = "";
        HttpPost httpPost = new HttpPost(url);
        CloseableHttpClient httpClient = HttpClients.createDefault();

            BasicResponseHandler handler = new BasicResponseHandler();
            StringEntity entity = new StringEntity(s, "utf-8");//解决中文乱码问题
            entity.setContentEncoding("UTF-8");
            entity.setContentType("application/json");
            httpPost.setEntity(entity);
            try {
                result = httpClient.execute(httpPost, handler);
                System.out.println(result);
            }catch (Exception e){
                e.printStackTrace();
            }



         //Object forObject = restTemplate.postForObject(url,json,Object.class);

    }

    /**
     * 网页授权access_token
     */
    public static String getAccessToken() {
        //RestTemplate restTemplate = new RestTemplate();


//        String access_token_url = "https://api.weixin.qq.com/cgi-bin/token";
//        String param="appid="+appID+"&secret="+appsecret+"&grant_type=client_credential";
//        String s = HttpUtil.sendGet(access_token_url, param);
//        JSONObject jsonObject = JSONObject.fromObject(s);
//        String emailObject = jsonObject.get("access_token").toString();
//        return emailObject;



        RestTemplate restTemplate = new RestTemplate();
        String access_token_url = "https://api.weixin.qq.com/cgi-bin/token?appid="+appID+"&secret="+appsecret+"&grant_type=client_credential";
        Object forObject = restTemplate.getForObject(access_token_url, Object.class);
        JSONObject jsonToken = JSONObject.fromObject(forObject);
        String token = null;
        if (StringUtils.isNotBlank(String.valueOf(jsonToken))) {
            token = jsonToken.getString("access_token");
        }
        return token;

    }


}


